package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.sdf
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.model.NotifyHistoryFileListModel
import kotlinx.android.synthetic.main.frag_transfer_history.*

/**
 * 传输历史
 */
class TransferHistoryFragment : BaseFragment() {
    private var taskId: String = ""
    private var messageType: String = ""

    companion object {
        fun newInstance(taskId: String, messageType: String): TransferHistoryFragment {
            val target = TransferHistoryFragment()
            val args = Bundle()
            args.putString("taskId", taskId)
            args.putString("messageType", messageType)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_transfer_history
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        arguments?.apply {
            taskId = getString("taskId")
            messageType = getString("messageType")
        }
        fetchInfo()
    }

    private fun updateView(data: NotifyHistoryFileListModel) {
        data.resultMaps[0]?.apply {
            tv_sponsor.text = this.name
            tv_time.text = sdf().format(this.sendTime)
            tv_transfer_to.text = this.sendUserName
            tv_transfer_file.text = this.fileList.toString()
            tv_remark.text = this.description
            tv_state.text = when (this.state) {
                "1" -> "传输中"
                "2" -> "传输完成"
                else -> "传输中"
            }
        }
    }

    @SuppressLint("CheckResult")
    private fun fetchInfo() {
        val params = mutableMapOf(
                Pair(Const.qr_taskId, taskId),
                Pair(Const.qr_messageType, messageType)
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_notify_history_file_info}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, NotifyHistoryFileListModel::class.java)?.apply {
                                if (code == "200") {
                                    updateView(this)
                                } else {
                                    error(msg)
                                }
                            }
                        },
                        {
                            hideLoader()
                            it.printStackTrace()
                            report(it)
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )

    }
}